var util=require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    percent:0,
    currentPrice:0,
    zhuliao:[
      {name:'油条',value:'油条',checked:true,price:1},
      {name:'脆饼',value:'脆饼',checked:false,price:1},
      {name:'蜜果',value:'蜜果',checked:false,price:1.5},
    ],
    fuliao:[
      {name:'牛肉粒',value:'牛肉粒',checked:false,price:1.5},
      {name:'鸡蛋',value:'鸡蛋',checked:false,price:1},
      {name:'火腿肠',value:'火腿肠',checked:false,price:1},
      {name:'鱿鱼丝',value:'鱿鱼丝',checked:false,price:1.5},
      {name:'蔬菜',value:'蔬菜',checked:false,price:1},
      {name:'辣条',value:'辣条',checked:false,price:1}
    ],
    fenliang:[
      {name:'单人份',prcie:1},
      {name:'双人份',prcie:1.8},
      {name:'三人份',prcie:2.7}
    ],
    index:0,
    la:'不辣',
    total:{'zhuliaoPrice':1,'fuliaoPrice':0,fenliang:1},
    song:false,
    date:'',
    time:'',
    region:[]
  },

  updatePrice:function(){
    var total=this.data.total;
    var price=(4+total.zhuliaoPrice+total.fuliaoPrice)*total.fenliang;
    this.setData({
      currentPrice: price.toFixed(1),
      percent:Math.round(price*100/33.75)
    })
  },
  radioChange:function(e){
    //console.log(e)
      var zhuliao=this.data.zhuliao;
      for(var i=0;i<zhuliao.length;i++)
      {
        if(zhuliao[i].value==e.detail.value){
          this.data.total.zhuliaoPrice=zhuliao[i].price;
          break;
        }
      }
      this.updatePrice();
  },
  checkboxChange :function (e) {
   // console.log(e)
    var value=e.detail.value;
    var fuliao=this.data.fuliao;
    var fuliaoPrice=0;
    for(var i=0;i<value.length;i++){
      for(var j=0;j<fuliao.length;j++){
        if(value[i]==fuliao[j].value){
          fuliaoPrice+=fuliao[j].price;
          break;
        }
      }
    }
    this.data.total.fuliaoPrice=fuliaoPrice;
    this.updatePrice();
  },
  pickerChange:function (e) {
    //console.log(e)
    this.data.total.fenliang=this.data.fenliang[e.detail.value].prcie;
    this.setData(
      {
        index:e.detail.value
      })
      this.updatePrice();
  },
  selectLa:function (value) {
    var la;
    if(value==0){
      la="不辣";
    }else if(value<=10){
      la="点点辣";
    }else if(value<=20){
      la="微辣";
    } else if(value<=30){
      la="小辣";
    }else if(value<=60){
      la="中辣";
    }else if(value<=90){
      la="大辣";
    }else{
      la="变态辣";
    }
    return la;
  },
  sliderChange:function (e) {
    //console.log(e);
    var value=e.detail.value;
    this.setData({
      la:this.selectLa(value)
    })
  },
  switchChang:function (e) {
   // console.log(e);
    this.setData({
      song:e.detail.value
    })
    //console.log(util.formatDate(new Date()));
    //console.log(util.formatTime(new Date()));
    this.setData({
      date:util.formatDate(new Date()),
      time:util.formatTime(new Date()),
    })
  },

  dateChange:function (e) {
    this.setData({
      date:e.detail.value
    })
  },
  timeChange:function (e) {
    this.setData({
      time:e.detail.value
    })
  },
  regionchange:function (e) {
    this.setData({
      region:e.detail.value
    })
  },
  reset:function (e) {
    var zhuliao=this.data.zhuliao;
    zhuliao[0].checked=true;
    var total=this.data.total;
    total.zhuliaoPrice=zhuliao[0].price;
    total.fuliaoPrice=0;
    total.fenliang=1;
    this.setData({
      zhuliao:zhuliao,
      song:false,
      index:0,
      la:'不辣',
      region:[]
    })
    this.updatePrice();
  },
  fukuan:function (e) {
    console.log(e)
    var allData=e.detail.value;
    var msg="煎饼果子总价格："+ this.data.currentPrice + '元\n';
    msg += '主料：'+allData.zhuliao+'\n';
    msg += '辅料：'+allData.fuliao.join(',')+'\n';
    msg += '份量：'+this.data.fenliang[allData.fenliang].name+'\n';
    msg += '辣度：'+this.selectLa(allData.la)+'\n';
    msg += '备注'+allData.beizhu+'\n';
    msg += '配送:'+(allData.peisong?'是':'否')+'\n';
    if(allData.peisong){
      msg += '联系电话：'+ allData.phone+'\n';
      msg+='配送日期：'+allData.date+'\n';
      msg+='配送时间'+allData.time+'\n';
      msg+='配送地址：'+allData.region.join('')+allData.address+'\n';
    }
    wx.navigateTo({
      url: '../test/test?msg='+msg,
    })
  },
  tijiao:function (e) {
    var allData=e.detail.value;
    allData.fenliang=this.data.fenliang[allData.fenliang].name;
    allData.la=this.selectLa(allData.la);
    if(allData.peisong){
        allData.address=allData.region.join('')+allData.address;
    }
    wx.navigateTo({
      url: '../about/about',
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromIndex', { data:allData })
      } 
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.updatePrice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})